package com.thinkingstar.iads.projectcenter.controller;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import com.google.common.io.Files;
import com.thinkingstar.iads.common.utils.DownloadFileUtil;
import com.thinkingstar.iads.common.utils.FileCommonUtil;
import com.thinkingstar.iads.common.utils.InitSystemProperties;
import com.thinkingstar.iads.common.utils.POIFileUtil;
import com.thinkingstar.iads.common.utils.PictureUtil;
import com.thinkingstar.iads.projectcenter.entity.PcProjectFile;
import com.thinkingstar.iads.projectcenter.entity.dao.DaoPcProjectFile;

@IocBean
@At("/projectCenter/previewCad")
public class ProjectPreviewCadController {
	
	@Inject
	private DaoPcProjectFile daoPcProjectFile;
	
	
	@At("/home/?")
	@Ok("beetl:/include/mxcad/mxcad.html")
	public NutMap home(String file_id , HttpServletRequest request){
		//将文件copy到web服务器路径下面,并返回路径
		PcProjectFile pcProjectFile = daoPcProjectFile.fetch(file_id);
		if(pcProjectFile != null){
			String relative_path = pcProjectFile.getRelativePaths();
			String file_path = POIFileUtil.getAbFileNameByRepath(relative_path);
			return new NutMap("url",copyFile(file_path,request));
		}
		return new NutMap("url", "");
		
	}
	
	public static String copyFile(String fileNameFrom , HttpServletRequest request){
		///opt/tomcat8/webapps/dwg
		String file_name = PictureUtil.createFileName("dwg");
		//文件地址
		String path = InitSystemProperties.getProjectPath();
		if(System.getProperty("os.name").toLowerCase().startsWith("win")){  //是win系统
			path = "e:";
		}
		String web_path = File.separator+path+File.separator+"tomcat8"+File.separator+"webapps"+File.separator+"dwg"+File.separator+file_name;
	try {
		Files.copy(new File(fileNameFrom), new File(web_path));
		} catch (IOException e) {
			e.printStackTrace();
		}
	return "http://"+InitSystemProperties.getPreviewCadServerIP()+":"+
    	InitSystemProperties.getPreviewCadServerPort()+"/dwg/"+file_name;
	}

}
